import { IConfig, IPlugin } from 'umi-types';
import defaultSettings from './defaultSettings'; // https://umijs.org/config/

import slash from 'slash2';
import webpackPlugin from './plugin.config';

const { pwa, primaryColor } = defaultSettings; // preview.pro.ant.design only do not use in your production ;
// preview.pro.ant.design 专用环境变量，请不要在你的项目中使用它。

const { ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION } = process.env;
const isAntDesignProPreview = ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site';
const plugins: IPlugin[] = [
  [
    'umi-plugin-react',
    {
      antd: true,
      dva: {
        hmr: true,
      },
      locale: {
        // default false
        enable: true,
        // default zh-CN
        default: 'zh-CN',
        // default true, when it is true, will use `navigator.language` overwrite default
        baseNavigator: true,
      },
      dynamicImport: {
        loadingComponent: './components/PageLoading/index',
        webpackChunkName: true,
        level: 3,
      },
      pwa: pwa
        ? {
            workboxPluginMode: 'InjectManifest',
            workboxOptions: {
              importWorkboxFrom: 'local',
            },
          }
        : false, // default close dll, because issue https://github.com/ant-design/ant-design-pro/issues/4665
      // dll features https://webpack.js.org/plugins/dll-plugin/
      // dll: {
      //   include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
      //   exclude: ['@babel/runtime', 'netlify-lambda'],
      // },
    },
  ],
  [
    'umi-plugin-pro-block',
    {
      moveMock: false,
      moveService: false,
      modifyRequest: true,
      autoAddMenu: true,
    },
  ],
]; // 针对 preview.pro.ant.design 的 GA 统计代码

if (isAntDesignProPreview) {
  plugins.push([
    'umi-plugin-ga',
    {
      code: 'UA-72788897-6',
    },
  ]);
  plugins.push([
    'umi-plugin-pro',
    {
      serverUrl: 'https://ant-design-pro.netlify.com',
    },
  ]);
}

export default {
  plugins,
  block: {
    defaultGitUrl: 'https://github.com/ant-design/pro-blocks',
  },
  hash: true,
  targets: {
    ie: 11,
  },
  devtool: isAntDesignProPreview ? 'source-map' : false,
  // umi routes: https://umijs.org/zh/guide/router.html
  routes: [
    {
      path: '/user',
      component: '../layouts/UserLayout',
      routes: [
        {
          name: 'login',
          path: '/user/login',
          component: './user/login',
        },
        {
          name: 'register',
          path: '/user/register',
          component: './user/register',
        },
        {
          name: 'forgotPsd',
          path: '/user/forgotPsd',
          component: './user/forgotPsd',
        },
      ],
    },
    {
      path: '/pay',
      component: '../layouts/UserLayout',
      routes: [
        {
          name: 'pay',
          path: '/pay/payConfirm',
          component: './pay/payConfirm',
        },
      ],
    },
    {
      path: '/manage',
      component: '../layouts/BasicLayout',
      // authority: ['kimsadsjhdj'],
      routes: [
        // 个人中心
        {
          name: 'person',
          path: '/manage/person',
          icon: 'user',
          routes: [
            // 概览
            // {
            //   name: 'analysis',
            //   path: '/manage/person/analysis',
            //   component: './manage/dashboard/analysis',
            // },
            // 工作台
            {
              name: 'workplace',
              path: '/manage/person/workplace',
              component: './manage/dashboard/workplace',
            }, // 我的消息
            {
              name: 'message',
              path: '/manage/person/message',
              component: './manage/message',
            }, // 密码修改
            {
              name: 'password',
              path: '/manage/person/password',
              component: './manage/password',
            }, // 基本信息
            {
              name: 'baseinfo',
              path: '/manage/person/baseinfo',
              component: './manage/userInfo',
            },
          ],
        }, // 企业中心
        {
          name: 'enterprise',
          icon: 'heat-map',
          path: '/manage/enterprise',
          routes: [
            // 我的企业
            {
              name: 'my',
              path: '/manage/enterprise/my',
              component: './manage/enterprise/myEnterprise',
            }, // 加入他人创建的企业
            {
              name: 'join',
              path: '/manage/enterprise/join',
              component: './manage/enterprise/joinEnterprise',
            }, // 企业成员加入申请审批
            {
              name: 'approval',
              path: '/manage/enterprise/approval',
              component: './manage/enterprise/approval',
            }, // 企业成员管理
            {
              name: 'member',
              path: '/manage/enterprise/member',
              component: './manage/enterprise/member',
            }, // 对账明细查询
            {
              name: 'accountRecord',
              path: '/manage/enterprise/accountRecord',
              component: './manage/enterprise/accountRecord',
            },
          ],
        }, // 订单中心
        {
          name: 'order',
          icon: 'shopping-cart',
          path: '/manage/order',
          routes: [
            // 代理服务订单
            {
              name: 'agent',
              path: '/manage/order/agent',
              component: './manage/order/agent',
            },
          ],
        },
        // 特色服务
        {
          name: 'service',
          icon: 'crown',
          path: '/manage/service',
          routes: [
            {
              //一键生成报关资料
              name: 'customsInfoGenerateManagement',
              path: '/manage/service/customsInfoGenerateManagement',
              routes: [
                // 一键生成报关资料
                {
                  name: 'customsInfoGenerate',
                  path: '/manage/service/customsInfoGenerateManagement/customsInfoGenerate',
                  component: './manage/service/customsInfoGenerateManagement/customsInfoGenerate',
                }, // 国外买家信息查询
                // 报关抬头
                {
                  name: 'declareManagement',
                  path: '/manage/service/customsInfoGenerateManagement/declareManagement',
                  component: './manage/service/customsInfoGenerateManagement/declareManagement',
                },
                // 开票工厂
                {
                  name: 'invoiceManagement',
                  path: '/manage/service/customsInfoGenerateManagement/invoiceManagement',
                  component: './manage/service/customsInfoGenerateManagement/invoiceManagement',
                },
                // 产品订单
                {
                  name: 'productManagement',
                  path: '/manage/service/customsInfoGenerateManagement/productManagement',
                  component: './manage/service/customsInfoGenerateManagement/productManagement',
                },
                // 境外贸易商
                {
                  name: 'tradersManagement',
                  path: '/manage/service/customsInfoGenerateManagement/tradersManagement',
                  component: './manage/service/customsInfoGenerateManagement/tradersManagement',
                },
              ],
            },

            {
              name: 'foreignTraderQuery',
              path: '/manage/service/foreignTraderQuery',
              component: './manage/service/foreignTraderQuery',
            }, // 信用证审单制单
            {
              name: 'credit',
              path: '/manage/service/credit',
              component: './manage/service/credit',
            }, // 出口货款融资
            {
              name: 'creditFinance',
              path: '/manage/service/creditFinance',
              component: './manage/service/creditFinance',
            }, // 远期信用证融资
            {
              name: 'creditLoan',
              path: '/manage/service/creditLoan',
              component: './manage/service/creditLoan',
            }, // 出口货款融资
            {
              name: 'applyAmount',
              path: '/manage/service/applyAmount',
              component: './manage/service/applyAmount',
            }, // 全球货款催收

            {
              //保险
              name: 'insurance',
              path: '/manage/service/insurance',
              routes: [
                // 代理服务订单
                {
                  name: 'creditInsurance',
                  path: '/manage/service/insurance/creditInsurance',
                  component: './manage/service/insurance/creditInsurance',
                },
                {
                  name: 'policyInsurance',
                  path: '/manage/service/insurance/policyInsurance',
                  component: './manage/service/insurance/policyInsurance',
                }, // 货运险
              ],
            },
          ],
        }, // {
        //   name: 'config',
        //   path: '/manage/config',
        //   routes: [
        //     {
        //       name: 'person.settings',
        //       path: '/manage/config/person/settings',
        //       component: './manage/config/person/settings',
        //     },
        //     {
        //       name: 'enterprise.settings',
        //       path: '/manage/config/enterprise/settings',
        //       component: './manage/config/enterprise/settings',
        //     },
        //   ],
        // },
        {
          name: 'finance',
          icon: 'transaction',
          path: '/manage/finance',
          routes: [
            {
              name: 'finance',
              path: '/manage/finance/foreignCredit',
              component: './foreign/credit',
            },

            {
              hideInMenu: true,
              name: 'foreignCreditDetail',
              path: '/manage/finance/foreignCreditDetail',
              component: './foreign/credit/detail',
            },
            {
              hideInMenu: true,
              name: 'foreignCreditApply',
              path: '/manage/finance/foreignCreditApply',
              component: './foreign/credit/apply',
            },
            {
              // hideInMenu: true,
              name: 'foreignCreditRecord',
              path: '/manage/finance/foreignCreditRecord',
              component: './foreign/credit/record',
            },
          ],
        },

        // {
        //   //物流服务
        //   name: 'logistics',
        //   icon: 'table',
        //   path: '/manage/logistics',
        //   routes: [
        //     {
        //       name: 'air-transport',
        //       path: '/manage/logistics/air-transport', //空运查询
        //       component: './manage/logistics/air-transport',
        //     },
        //     {
        //       name: 'sea-transport', //海运查询
        //       path: '/manage/logistics/sea-transport',
        //       component: './manage/logistics/sea-transport',
        //     },
        //     {
        //       name: 'trailer-transport', //拖运查询
        //       path: '/manage/logistics/trailer-transport',
        //       component: './manage/logistics/trailer-transport',
        //     },
        //     {
        //       hideInMenu: true,
        //       name: 'air-online-booking', //空运订舱
        //       path: '/manage/logistics/air-online-booking',
        //       component: './manage/logistics/air-online-booking',
        //     },
        //     {
        //       hideInMenu: true,
        //       name: 'sea-online-booking', //海运订舱
        //       path: '/manage/logistics/sea-online-booking',
        //       component: './manage/logistics/sea-online-booking',
        //     },
        //     {
        //       hideInMenu: true, //拖车订舱
        //       name: 'trailer-online-booking',
        //       path: '/manage/logistics/trailer-online-booking',
        //       component: './manage/logistics/trailer-online-booking',
        //     },
        //   ],
        // },

        {
          component: './404',
        },
      ],
    },
    {
      path: '/',
      component: '../layouts/PortalLayout',
      routes: [
        {
          path: '/',
          component: './home',
        },
        {
          path: '/service/trade',
          component: './product/service/trade',
        },
        {
          path: '/service/finance',
          component: './product/service/finance',
        },
        {
          path: '/service/production-guide',
          component: './product/service/production-guide',
        },
        {
          path: '/service/product-guide',
          component: './product/product-guide',
        },
        // {
        //   path: '/service/logistics',
        //   component: './product/service/logistics',
        // },
        {
          path: '/product/software',
          component: './product/item/index',
        },
        {
          path: '/product/software/price',
          component: './product/item/price',
        },

        {
          path: '/dynamic/news',
          component: './dynamic/news',
        },
        {
          path: '/dynamic/news-detail/:id',
          component: './dynamic/news-detail',
        },

        {
          path: '/introduce',
          component: './introduce',
        },

        {
          //物流服务
          name: 'logistics',
          icon: 'table',
          path: '/service/logistics',
          routes: [
            {
              name: 'air-transport',
              path: '/service/logistics/air-transport', //空运查询
              component: './manage/logistics/air-transport',
            },
            {
              name: 'sea-transport', //海运查询
              path: '/service/logistics/sea-transport',
              component: './manage/logistics/sea-transport',
            },
            {
              name: 'trailer-transport', //拖运查询
              path: '/service/logistics/trailer-transport',
              component: './manage/logistics/trailer-transport',
            },
            {
              hideInMenu: true,
              name: 'air-online-booking', //空运订舱
              path: '/service/logistics/air-online-booking',
              component: './manage/logistics/air-online-booking',
            },
            {
              hideInMenu: true,
              name: 'sea-online-booking', //海运订舱
              path: '/service/logistics/sea-online-booking',
              component: './manage/logistics/sea-online-booking',
            },
            {
              hideInMenu: true, //拖车订舱
              name: 'trailer-online-booking',
              path: '/service/logistics/trailer-online-booking',
              component: './manage/logistics/trailer-online-booking',
            },
          ],
        },
      ],
    },
    {
      component: './404',
    },
  ],
  // Theme for antd: https://ant.design/docs/react/customize-theme-cn
  theme: {
    'primary-color': primaryColor,
  },
  define: {
    ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION:
      ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION || '',
    // preview.pro.ant.design only do not use in your production ; preview.pro.ant.design 专用环境变量，请不要在你的项目中使用它。
    //生产环境
    // BASEDATA_PATH: 'https://www.zbtservices.com:8482',
    // AC_PATH: 'https://www.zbtservices.com:8483',
    // MC_PATH: 'https://www.zbtservices.com:8481',
    // LOGISTICS_PATH: 'https://www.zbtservices.com:8494',
    // PORTAL_PATH: 'https://www.zbtservices.com',
    // LESSEE_CODE: '0001',

    // AC_PATH: "http://47.99.63.16:8083",
    // BASEDATA_PATH: "http://47.99.63.16:8082",
    // MC_PATH: "http://47.99.63.16:8081",
    // LOGISTICS_PATH: 'http://47.99.61.32:8094',
    // PORTAL_PATH: 'http://47.99.63.16',

    //测试环境
    BASEDATA_PATH: 'http://47.99.66.242:8082',
    AC_PATH: 'http://47.99.66.242:8080',
    MC_PATH: 'http://47.99.66.242:8081',
    LOGISTICS_PATH: 'http://47.99.66.242:8093',
    PORTAL_PATH: 'http://47.99.66.242:8084',

    LESSEE_CODE: '0001',

    //开发环境
    // AC_PATH: 'http://localhost:8080',
    // MC_PATH: 'http://localhost:8081',
    // BASEDATA_PATH: 'http://localhost:8082',
    // LOGISTICS_PATH: 'http://localhost:8093',
    // PORTAL_PATH: 'http://localhost:8098',
    // LESSEE_CODE: '0001',
  },
  ignoreMomentLocale: true,
  lessLoaderOptions: {
    javascriptEnabled: true,
  },
  disableRedirectHoist: true,
  cssLoaderOptions: {
    modules: true,
    getLocalIdent: (
      context: {
        resourcePath: string;
      },
      _: string,
      localName: string,
    ) => {
      if (
        context.resourcePath.includes('node_modules') ||
        context.resourcePath.includes('ant.design.pro.less') ||
        context.resourcePath.includes('global.less')
      ) {
        return localName;
      }

      const match = context.resourcePath.match(/src(.*)/);

      if (match && match[1]) {
        const antdProPath = match[1].replace('.less', '');
        const arr = slash(antdProPath)
          .split('/')
          .map((a: string) => a.replace(/([A-Z])/g, '-$1'))
          .map((a: string) => a.toLowerCase());
        return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-');
      }

      return localName;
    },
  },
  manifest: {
    basePath: '/',
  },
  chainWebpack: webpackPlugin,
  // proxy: {
  //   '/server/api/': {
  //     target: 'https://www.zbtservices.com:8483/',
  //     changeOrigin: true,
  //     pathRewrite: { '^/server': '/' },
  //   },
  // },
} as IConfig;
